package com.tf_cut.backend.agent.mobile.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.tf_cut.backend.agent.mobile.vo.AgentHomeVo;
import com.tf_cut.backend.agent.mobile.vo.MobileLoginVo;
import com.tf_cut.backend.agent.service.*;
import com.tf_cut.backend.agent.vo.AgentAccountDetailVo;
import com.tf_cut.backend.agent.vo.AgentAndAccountVo;
import com.tf_cut.backend.common.pojo.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/backend/agent/mobile")
public class AgentMobileController {

	@Autowired
	private AccountDetailService detailService;

	@Autowired
	private AgentAccountService agentAccountService;
	
	@Autowired
	private AgentService agentService;
	
	@Autowired
	private AgentAndAccountService agentAndAgentService;

	@Autowired
	private AgentCashOrderService agentCashOrderService;





	@PostMapping("/agentInfoWithLogin")
	public  Result<MobileLoginVo> login(@RequestBody MobileLoginVo mobileLoginVo){

		MobileLoginVo.MobileLoginVoBuilder builder = MobileLoginVo.builder();

		Result<List<AgentAndAccountVo>> list = agentService.list(AgentAndAccountVo.builder().loginId(mobileLoginVo.getLoginId()).build());

		if(list.isFlag()&&!list.getData().isEmpty()) {
			AgentAndAccountVo agentAndAccountVo = list.getData().get(0);

			AgentAndAccountVo agentAccountIdWithAgentId = this.agentAccountService.findAgentAccountIdWithAgentId(agentAndAccountVo.getAgentId());
			if(agentAccountIdWithAgentId!=null){
				builder.agentType(agentAccountIdWithAgentId.getAgentType());
			}
			builder.agentId(agentAndAccountVo.getAgentId())
					.agentName(agentAndAccountVo.getAgentName())
					.agentPerson(agentAndAccountVo.getAgentPerson())
					.agentPhone(agentAndAccountVo.getAgentPhone());


		}

		return Result.of(builder.build());

	}


	
	// 添加代理商
	@GetMapping("/home")
    public Result<AgentHomeVo> home(@RequestParam String agentId) {

		Result<AgentHomeVo> result=null;
		AgentHomeVo agentHomeVo = new AgentHomeVo();


		try {
			Result<AgentAndAccountVo> agentAndAccountVoResult = agentService.get(agentId);
			if(!agentAndAccountVoResult.isFlag()){
				result = Result.of(agentHomeVo);
				result.setMsg("代理商不存在");
				result.setFlag(false);
			}
			//代理商名称
			AgentAndAccountVo agentvo = agentAndAccountVoResult.getData();
			BeanUtils.copyProperties(agentvo, agentHomeVo);
			//总金额
			AgentAndAccountVo agentAndAccountVo = agentAccountService.findAgentAccountIdWithAgentId(agentId);
			if(null!=agentAndAccountVo){

				agentHomeVo.setAgentAccountBalance(agentAndAccountVo.getAgentAccountBalance());

				//本月总收入 和总支出
				//本月第一天和最后一天
				SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
				Calendar c = Calendar.getInstance();
				c.add(Calendar.MONTH, 0);
				c.set(Calendar.DAY_OF_MONTH,1);//1:本月第一天
				String startDate= format.format(c.getTime());
				Calendar ca = Calendar.getInstance();
				ca.set(Calendar.DAY_OF_MONTH, ca.getActualMaximum(Calendar.DAY_OF_MONTH));
				String endDate= format.format(ca.getTime());

				List<AgentAccountDetailVo> agentAccountDetailNoPage = agentAndAgentService.findAgentAccountDetailNoPage(AgentAccountDetailVo.builder().agentAccountId(agentAndAccountVo.getAgentAccountId())
						.startDate(startDate).endDate(endDate).build());

				if(!agentAccountDetailNoPage.isEmpty()){
					//交易方向，1：收入；-1：支出
					double income = agentAccountDetailNoPage.stream().filter(e -> e.getOperType().intValue() == 1).map(e -> e.getTradeMoney())
							.collect(Collectors.summarizingDouble(e -> e.doubleValue())).getSum();
					agentHomeVo.setIncomeMoneyWithMonth(new BigDecimal(income));

					double pay = agentAccountDetailNoPage.stream().filter(e -> e.getOperType().intValue() == -1).map(e -> e.getTradeMoney())
							.collect(Collectors.summarizingDouble(e -> e.doubleValue())).getSum();
					agentHomeVo.setPayMoneyWithMonth(new BigDecimal(pay));

				}else{
					agentHomeVo.setIncomeMoneyWithMonth(new BigDecimal(0));
					agentHomeVo.setPayMoneyWithMonth(new BigDecimal(0));
				}


			}else{
				//否则为0
				agentHomeVo.setAgentAccountBalance(new BigDecimal(0));
				agentHomeVo.setIncomeMoneyWithMonth(new BigDecimal(0));
				agentHomeVo.setPayMoneyWithMonth(new BigDecimal(0));
			}

			result = Result.of(agentHomeVo);




		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result = Result.of(agentHomeVo);
			result.setMsg(e.getMessage());
			result.setFlag(false);
		}


		return result;
    }



	}
